<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>对象化接口</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.extended.html">扩展接口</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="internals2.counter.counter-class.html">Counter</a></div>
 <div class="up"><a href="internals2.counter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="internals2.counter.examples.objective" class="section">
    <h2 class="title">对象化接口</h2>
    
    <p class="simpara">
     对象接口提供一个面向对象的方式来访问扩展接口。下面的例子显示了使用对象化接口实现上面的例子。此例子的输出，除了所显示“计数器无效!”变为由 PHP 发出的变量 <em>$counter_three</em> 不是对象的警告信息以外，其他完全一样。此例子展示了定义在扩展中定义的 <strong class="classname">Counter</strong>
     的子类，并对实例变量而不是用方法存取计数器的值。
    </p>
    
    <div class="example" id="internals2.counter.examples.objective.ex">
     <p><strong>Example #1 &quot;counter&quot; 的对象化接口</strong></p>
     
     <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">MyCounter&nbsp;</span><span style="color: #007700">extends&nbsp;</span><span style="color: #0000BB">Counter<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">()&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"计数器的名称为&nbsp;'%s'，且%s进行持久化。其当前值为&nbsp;%d.\n"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMeta</span><span style="color: #007700">(</span><span style="color: #0000BB">COUNTER_META_NAME</span><span style="color: #007700">),<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMeta</span><span style="color: #007700">(</span><span style="color: #0000BB">COUNTER_META_IS_PERSISTENT</span><span style="color: #007700">)&nbsp;?&nbsp;</span><span style="color: #DD0000">''&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #DD0000">'不'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">Counter</span><span style="color: #007700">::</span><span style="color: #0000BB">setCounterClass</span><span style="color: #007700">(</span><span style="color: #DD0000">"MyCounter"</span><span style="color: #007700">);<br />if&nbsp;((</span><span style="color: #0000BB">$counter_one&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">::</span><span style="color: #0000BB">getNamed</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">))&nbsp;===&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$counter_one&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"one"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_PERSIST</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">$counter_one</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bumpValue</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;不允许直接&nbsp;"set"&nbsp;值<br /></span><span style="color: #0000BB">$counter_two&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"two"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_three&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">::</span><span style="color: #0000BB">getNamed</span><span style="color: #007700">(</span><span style="color: #DD0000">"three"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_four&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Counter</span><span style="color: #007700">(</span><span style="color: #DD0000">"four"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_PERSIST&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_SAVE&nbsp;</span><span style="color: #007700">|&nbsp;</span><span style="color: #0000BB">COUNTER_FLAG_NO_OVERWRITE</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bumpValue</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$counter_one</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_two</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_three</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$counter_four</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">printCounterInfo</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
     </div>

    </div>
   </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="internals2.counter.examples.extended.html">扩展接口</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="internals2.counter.counter-class.html">Counter</a></div>
 <div class="up"><a href="internals2.counter.examples.html">范例</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
